#include "metestation.h"

#include <QDebug>
#include <QSerialPort>


//const int METESTATION_PROTOCOLALL_LENGTH = 136;


MeteStation::MeteStation(QObject* parent)
    :m_port(new QSerialPort(this))
{
    connect(this, &MeteStation::protocol, this, [=](QByteArray ary){
        QString str = ary;
        QStringList protocoList = str.split(' ');
        MeteStationProtocolAll protocol;
        protocol.date = QDate::fromString(protocoList[2], "yyyy-MM-dd");
        protocol.time = QTime::fromString(protocoList[3], "hh:mm:ss");

        protocol.temperature = protocoList[4].toInt()/10;
        protocol.humidity = protocoList[5].toInt()/10;
        protocol.pressure = protocoList[6].toInt()/10;
        protocol.one_minute_maxWindSpeed = protocoList[7].toInt()/10;
        protocol.one_minute_windDirection = protocoList[8].toInt()/10;
        protocol.two_minute_avgWindSpeed = protocoList[9].toInt()/10;
        protocol.two_minute_avgWindDirection = protocoList[10].toInt()/10;
        protocol.ten_minute_avgWindSpeed = protocoList[11].toInt()/10;
        protocol.ten_minute_avgWindDirection = protocoList[12].toInt()/10;
        protocol.minute_precipitation = protocoList[13].toInt()/10;
        protocol.hour_precipitation = protocoList[14].toInt()/10;
        protocol.aux_minute_precipitation = protocoList[15].toInt()/10;
        protocol.aux_hour_precipitation = protocoList[16].toInt()/10;
        protocol.road_temerature = protocoList[17].toInt()/10;
        protocol.atmosphere_electric_field = protocoList[18].toInt()/10;
        protocol.total_solar_radiation = protocoList[19].toInt()/10;
        protocol.dew_point_temperature = protocoList[20].toInt()/10;
        protocol.water_vapor_pressure = protocoList[21].toInt()/10;
        protocol.voltage = protocoList[22].toInt()/10;
        protocol.electricity_quantity = protocoList[23].toInt();
        protocol.tilt_angle = protocoList[24].toInt()/10;

        emit updateView(protocol);
    });
}

MeteStation* MeteStation::instance = new MeteStation;

MeteStation *MeteStation::getInstance()
{
    return instance;
}

bool  MeteStation::openDevice(QString portName)
{
    m_port->setPortName(portName);
    m_port->setBaudRate(9600);
    m_port->setDataBits(QSerialPort::Data8);
    m_port->setStopBits(QSerialPort::OneStop);
    m_port->setParity(QSerialPort::NoParity);
    m_port->setFlowControl(QSerialPort::NoFlowControl);
    m_port->setReadBufferSize(4096);
    if(m_port->open(QSerialPort::ReadWrite)){
        qDebug()<<"打开成功";
        connect(this->m_port, &QSerialPort::errorOccurred, this, [=](QSerialPort::SerialPortError err){
            qDebug()<<"错误信息 : "<<this->m_port->errorString();
        });
        connect(this->m_port, &QSerialPort::readyRead, this, [=](){
            if(this->m_port->bytesAvailable()){
                QByteArray ary = this->m_port->readAll();
                qDebug()<<"rec arr:"<<ary<<"ary.length: "<<ary.length();
                this->m_readBuffer.append(ary);
                int index = this->m_readBuffer.indexOf("LWS");
                //qDebug()<<"index:"<<index<<"  m_readBuffer:"<<this->m_readBuffer;
                // |多余数据|LWS|正确数据|，删除第一次LWS前的所有数据
                if(index != 0){
                    this->m_readBuffer.remove(0, index);
                }
                if(this->m_readBuffer.length() >= METESTATION_PROTOCOLALL_LENGTH){
                    int index = this->m_readBuffer.indexOf("LWS");
                    qDebug()<<"index:"<<index<<"  m_readBuffer:"<<this->m_readBuffer;
                    if(index >= 0){
                        QByteArray result = this->m_readBuffer.mid(index, METESTATION_PROTOCOLALL_LENGTH);
                        //qDebug()<<"result : "<<result;
                        emit protocol(result);
                        this->m_readBuffer.remove(index, METESTATION_PROTOCOLALL_LENGTH);
                    }
                }
            }
        });
        return true;
    }
    return false;
}

void MeteStation::closedevice()
{
    m_port->close();
}

MeteStation::~MeteStation()
{
    delete instance;
}


